<?php

/**
 * Created by PhpStorm daichen.
 * User: 橙橙同学
 * Date: 2017/11/22
 * Time: 11:00
 */
namespace app;

class MsParkApiTest
{
    const APPKEY = '';
    const HOST = 'http://121.199.49.43';

    protected static function getSign()
    {
        $ts = time();
        $signStr = $ts.self::APPKEY;
        return md5($signStr);
    }

    protected static function linUrl()
    {
        return self::HOST.'/api/MallCoo/';
    }

    public static function queryPark($plateNumber)
    {
        $url = self::linUrl().'?plateNo='.$plateNumber;
        $res = http_get($url);


        //如果返回false  http请求失败
        if(!$res)
        {
            $res = json_encode(array('msg'=>'HTTP请求错误'));
        }
        self::MarkResponseData($url,"",$res,'get');
    }

    public static function payPark()
    {
        $url = self::linUrl().'pay';
        $args = array(
            'feeId'=>'',
            'plateNo'=>'',
            'payTime'=>'',
            'parkingMinutes'=>'',
            'totalAmount'=>'',
            'unPayAmount'=>'',
            'amount'=>'',
            'deductionAmount'=>'',
            'paidAmount'=>'',
            'payChannel'=>'',
            'payType'=>'',
        );
        $postData = json_encode($args);
        $res = http_post($url,$postData);
        //如果返回false  http请求失败
        if(!$res)
        {
            $res = json_encode(array('msg'=>'HTTP请求错误'));
        }
        self::MarkResponseData($url,$postData,$res);
    }

    protected static function commonReturn($errCode,$msg)
    {
        echo "<p style='background: #d9edf7;margin-bottom: 1px;padding: 10px;border-radius: 5px'>返回码 {$errCode} 返回信息 {$msg}</p>";
    }


    protected static function MarkResponseData($url,$postData,$data,$request_type='post')
    {
        $insertRes = pdo_insert('api_record',array(
                'api'=>$url,
                'request'=>$postData,
                'response'=>$data,
                'create_time'=>time(),
                'apitype'=>'mspark',
                'request_type'=>$request_type,
            )
        );
        if($insertRes)
        {
            self::commonReturn(0,'接口'.$url.' 测试结果记录成功');
        }
        else{
            self::commonReturn(-1,'接口'.$url.' 测试结果记录失败');
        }
    }



}